package com.hceax.servlet;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.hceax.servlet.SysDataSource;

import com.hceax.servlet.User;

public class UserDAO {
	public User findUser(String sno){
		User user = null;
		Connection conn = null;
		try {
			//连接数据库
			conn = SysDataSource.getInstance().getConnection();
			//设定命令
			PreparedStatement pstmt = conn.prepareStatement("select * from user where sno = ?");
			pstmt.setString(1, sno);//指定命令参数
			ResultSet rs = pstmt.executeQuery();//执行并返回结果
			if(rs.next()){
			user = new User();
			user.setSno(rs.getString("sno"));
			user.setUsername(rs.getString("name"));
			user.setPassword(rs.getString("password"));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				if(conn!=null){
					conn.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
			}
		return user;
	}
}
